package com.security.one_way_encry.md5;

import ch.qos.logback.core.encoder.ByteArrayUtil;
import org.apache.commons.codec.binary.Hex;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

/**
 * @ClassName TestMD5ByJava
 * @Description 使用JDK进行MD5加密
 * @Detail detail
 * @Author MyPC
 * @Date 2020/9/23
 * @Version 1.0
 */
public class TestMD5ByJava {
    public static void main(String[] args) throws NoSuchAlgorithmException {
        String hello = encryptMD5("hello");
        System.out.println(hello);
    }

    public static String encryptMD5(String key) throws NoSuchAlgorithmException {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(key.getBytes());
        //转成MD5码
        byte[] digest = md5.digest();
        //转换成16进制的String字符串
        return Hex.encodeHexString(digest);
//        return ByteArrayUtil.toHexString(digest);
    }
}
